<template>
  <div class="login-panle">
    <header>登录界面</header>

    <el-tabs
      v-model="activeName"
      type="border-card"
      class="demo-tabs"
      stretch
      style="background-color;: transparent"
    >
      <el-tab-pane name="account">
        <template #label>
          <span class="custom-tabs-label">
            <el-icon><Avatar /></el-icon>
            <span>账号登录</span>
          </span>
        </template>
        <!-- 封装的form表单组件 -->
        <LoginAccount ref="AccountIns" @handleLoading="handleLoading"></LoginAccount>
      </el-tab-pane>

      <el-tab-pane name="phone">
        <template #label>
          <el-icon><Iphone /></el-icon>
          <span>手机号登录</span>
        </template>
        <LoginPhone ref="PhoneIns"></LoginPhone>
      </el-tab-pane>
    </el-tabs>

    <div class="more">
      <el-checkbox v-model="checkValue" label="记住密码" size="large" />
      <el-link type="primary">忘记密码</el-link>
    </div>

    <footer>
      <el-button
        style="width: 75%"
        type="primary"
        loading-icon="Eleme"
        :loading="isLoading"
        @click="handleLogin"
        >登录</el-button
      >
    </footer>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
// @ts-ignore
import LoginAccount from '../components/LoginAccount.vue'
import LoginPhone from './LoginPhone.vue'

import { MyStorage } from '@/utils/utils'
const activeName = ref('account')
const checkValue = ref<boolean>(MyStorage.get('checkValueStorage') || false)
// 获取组件实例,需要设置该组件的类型
/* 
LoginAccount为导入的组件，而该变量就是接受导出该组件的实例使用，
本质是一样的，但是不能直接使用typof直接接受该组件类型
所有组件的实例定义都是这样子：<InstanceType<typeof 组件>>
*/
const AccountIns = ref<InstanceType<typeof LoginAccount>>()
const PhoneIns = ref<InstanceType<typeof LoginPhone>>()
const isLoading = ref(false)

const handleLoading = (val: boolean) => {
  isLoading.value = val
}

/**
 * 点击登录接口
 */
const handleLogin = async () => {
  // 根据tab切换登录选择需要传递的数据
  if (activeName.value === 'account') {
    AccountIns.value?.verifyAccount(checkValue.value)
  } else {
    PhoneIns.value?.verifyPhone()
  }
}

watch(checkValue, (newVal) => {
  // 存储本地数据
  MyStorage.set('checkValueStorage', newVal)
})
</script>

<style lang="scss" scoped>
.login-panle {
  color: #6d6d6d;
  background-color: #363636;
  width: 100%;
  text-align: center;
  border-radius: 15px;
  padding: 10px;
  header {
    font-size: 30px;
    margin-bottom: 15px;
  }
  .more {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  footer {
    width: 100%;
  }
  .demo-tabs {
    .get-code {
      margin-top: 10px;
    }
  }
  :deep(.el-tabs--border-card > .el-tabs__header .el-tabs__item.is-active) {
    color: lightblue;
  }
  :deep(.el-tabs--border-card > .el-tabs__header .el-tabs__item:not(.is-disabled):hover) {
    color: lightblue;
  }
}
</style>
